/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import baseObjects.XML_Handler;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import pipeline.KeyboardCandidate;

/**
 *
 * @author yoav
 */
public class initDataHandler {

    private Hashtable<String, Integer> keyCodeMap = null;

    public void initKeyCodeMapFromFile(String fileName, Hashtable<String, myTextButton> keyboard) {
        keyCodeMap = new Hashtable<String, Integer>();
        Properties proper = new Properties();
        proper.clear();
        File f = new File(fileName);
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(f);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(initDataHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            proper.load(fis);
        } catch (IOException ex) {
            Logger.getLogger(initDataHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        scanAndParseProperties(proper, keyboard);
        try {
            fis.close();
        } catch (IOException ex) {
            Logger.getLogger(initDataHandler.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void initKeyCodeMapFromMem(KeyboardCandidate candidate, Hashtable<String, myTextButton> keyboard) {
        scanAndParseProperties(candidate.getKeysP(), keyboard);
    }

   
    private void scanAndParseProperties(Properties keyboardProp, Hashtable<String, myTextButton> keyboardButtons) {
        Properties proper = keyboardProp;
        System.out.println(proper.keySet() + " " + proper.keySet().size());
        for (Object key : proper.keySet()) {
            //the complete propertie value (the num)
            String mapCodeStr = ((String) proper.get(key)).trim();
            
            do_iterate(mapCodeStr,key,keyboardButtons);
            
        }

    }

    public Enumeration<String> get_enumerate() {
        return keyCodeMap.keys();
    }

    void saveFile(String file,Enumeration<myTextButton> tempKeys) {
         Properties proper = new Properties();
        proper.clear();
        while (tempKeys.hasMoreElements()) {
            
            myTextButton tempbut = tempKeys.nextElement();

            //String tpropVal="";
            if (!tempbut.getRegularLabel().equals("")){
                proper.setProperty(tempbut.getRegularLabel(),tempbut.getKeyId());
            }
            if (!tempbut.getShiftLabel().equals("")){//1
                proper.setProperty(tempbut.getShiftLabel(),Integer.toString(myTextButton.shiftId)+tempbut.getKeyId());
            }
            if (!tempbut.getAltLabel().equals("")){//2
                proper.setProperty(tempbut.getAltLabel(),Integer.toString(myTextButton.altId)+tempbut.getKeyId());
            }
            if (!tempbut.getCtrlLabel().equals("")){//3
                proper.setProperty(tempbut.getCtrlLabel(),Integer.toString(myTextButton.ctrlId)+tempbut.getKeyId());
            }
            
        }
         
        
      /////////////open
        File f = new File(file);
        FileOutputStream fis = null;
        try {
            fis = new FileOutputStream(f);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(initDataHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            proper.store(fis, "");
        } catch (IOException ex) {
            Logger.getLogger(initDataHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        ////////////close
        try {
            fis.close();
        } catch (IOException ex) {
            Logger.getLogger(initDataHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void do_iterate(String mapCodeStr,Object key,Hashtable<String, myTextButton> keyboardButtons) {
                   int keyType;
            String keyId = null;
            String[] bindings = mapCodeStr.split(",");
            boolean bindingHandle = false;
            //1.check for bindings in line:
            if (bindings.length > 1) {
                bindingHandle = true;
                mapCodeStr = bindings[0];
            }
            //2.check for alt/shift/cntrl regular line type:
            if (mapCodeStr.length() == 4) {
                keyType = Integer.parseInt(mapCodeStr.substring(0, 1));//the type of label reg/shift etc
                keyId = mapCodeStr.substring(1, mapCodeStr.length());//the actual button id
            } else {
                keyType = 0;
                keyId = mapCodeStr;
            }
            //System.err.println("key:"+((String) key)+" element:" +mapCode +" keytype:"+keyType+" keyId:"+keyId+" ("+mapCodeStr.length()+")");
            myTextButton tempKey = keyboardButtons.get(keyId);//the button
            //3.update binding:
            if (bindingHandle) {
                for (int i = 1; i < bindings.length; i++) {
                    int num = Integer.parseInt(bindings[i]);
                    switch (num) {
                        case myTextButton.shiftId:
                            tempKey.setBindingsToShift(true);
                            break;
                        case myTextButton.altId:
                            tempKey.setBindingsToAlt(true);
                            break;
                        case myTextButton.ctrlId:
                            tempKey.setBindingsToCtrl(true);
                            break;
                        default:
                            System.err.println("error at binding type, there is no binding num:" + num);
                            break;

                    }
               //     System.out.println(key + " found " + num);
                }

            }
            //4.update all button alternate labels
            switch (keyType) {
                case myTextButton.regularId:
                    tempKey.setRegularLabel((String) key);
                    break;
                case myTextButton.shiftId:
                    tempKey.setShiftLabel((String) key);
                    break;
                case myTextButton.altId:
                    tempKey.setAltLabel((String) key);
                    break;
                case myTextButton.ctrlId:
                    tempKey.setCtrlLabel((String) key);
                    break;
                default:
                    System.err.println("error in propetie file at keyId:" + keyId);
                    break;
            }

            //5 update keyId
            tempKey.setKeyId(keyId);


    }
}
